﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class Bacteria
    {
        int inputX, inputY, inputZ;
        public Bacteria()
        {
            int testCount = Int32.Parse(Console.ReadLine());

            for (int testCase = 1; testCase <= testCount; testCase++)
            {
                string[] input = Console.ReadLine().Split(' ');
                inputX = Int32.Parse(input[0]);
                inputY = Int32.Parse(input[1]);
                inputZ = Int32.Parse(input[2]);
                int[,,] drawing = new int[inputZ + 1, inputX + 1, inputY + 1];
                for (int z = 1; z <= inputZ; z++)
                {
                    for (int x = 1; x <= inputX; x++)
                    {
                        string input2 = Console.ReadLine();
                        for (int y = 1; y <= inputY; y++)
                        {
                            drawing[z, x, y] = (input2[y - 1] == '.') ? 0 : -1;
                        }
                    }
                }
                int[] bang = new int[inputZ + 1]; //방의 수

                List<Vitiation> vitiation = new List<Vitiation>(); //충돌 관계
                int bangNum = 1;
                for (int z = 1; z <= inputZ; z++)
                {
                    int index = 1;
                    for (int x = 1; x <= inputX; x++)
                    {
                        for (int y = 1; y <= inputY; y++)
                        {
                            if (GetBangCount(z, x, y, drawing, bangNum))
                            {
                                vitiation.Add(new Vitiation(z, bangNum));
                                bangNum++;
                                index++;
                            }
                        }
                    }
                    bang[z] = index - 1;
                }
                ////union
                
                
                for (int z = 2; z <= inputZ; z++)
                {
                    for (int x = 1; x <= inputX; x++)
                    {
                        for (int y = 1; y <= inputY; y++)
                        {
                            if (drawing[z - 1, x, y] > -1 && drawing[z, x, y] > -1)
                            {
                                Vitiation down = vitiation.Find(v=> v.Bang == drawing[z - 1, x, y]);
                                Vitiation up = vitiation.Find(v => v.Bang == drawing[z, x, y]);
                                down.AddVit(up.Bang);
                                up.AddVit(down.Bang);

                            }
                        }
                    }
                }
                //3개 이상과 충돌되는건 일단 무조건 지움
                
                for (int i = 0; i < vitiation.Count; i++)
                {

                    Vitiation vit = vitiation[i];
                    if (vit.Vit.Count > 3)
                    {
                        for (int j = 0; j < vit.Vit.Count; j++)
                        {
                            Vitiation v = vitiation.Find(b => b.Bang == vit.Vit[j]);
                            v.Vit.Remove(vit.Bang);

                        }
                        if (vit.Vit.Count > 0)
                        {
                            vitiation[i].Bang = -1;
                        }
                    }
                }
                vitiation.RemoveAll(v => v.Bang == -1);
                //bicolor 처리
                int result = 0;
                for (int i = 0; i < vitiation.Count; i++)
                {
                    if (vitiation[i].Odd == 0)
                    {
                        BiColor(vitiation, i, i + 1);
                        result += Math.Max(vitiation.Count(v => v.Odd == i + 1), vitiation.Count(v => v.Odd == -(i + 1)));
                    }
                }
                //int result = vitiation.Count(v => v.Odd == 0) + Math.Max(vitiation.Count(v => v.Odd == 1), vitiation.Count(v => v.Odd == 2));
                Console.WriteLine("Case #" + testCase + ": " + result);
                
            }
        }
        class Vitiation
        {
            public int Z { get; set; }
            public int Bang { get; set; }
            public List<int> Vit { get; set; }
            public int Odd { get; set; }
            public Vitiation(int z, int bang)
            {
                Z = z;
                Bang = bang;
                Vit = new List<int>();
            }
            public void AddVit(int bang)
            {
                if (!Vit.Contains(bang))
                {
                    Vit.Add(bang);
                }
            }
            public override string ToString()
            {
                string ret = string.Empty;
                ret = "Bang=" + Bang + ", Odd=" + Odd + ", Vit=";

                foreach (int v in Vit)
                {
                    ret += v + "_";
                }
                return ret;
            }
        }
        private void BiColor(List<Vitiation> vitiation, int index, int odd)
        {
            Vitiation now = vitiation[index];

            now.Odd = odd;
            odd *= -1;
            foreach(int childBang in now.Vit)
            {
                int childIndex = vitiation.FindIndex(v => v.Bang == childBang);
                if (vitiation[childIndex].Odd == 0)
                {
                    BiColor(vitiation, childIndex, odd);
                }
            }
        }
        private bool GetBangCount(int z, int x, int y, int[, ,] drawing, int index)
        {
            if (drawing[z, x, y] == 0)
            {
                drawing[z, x, y] = index;
                if (x < inputX && drawing[z, x + 1, y] == 0)
                {
                    GetBangCount(z, x + 1, y, drawing, index);
                }
                if (x > 1 && drawing[z, x - 1, y] == 0)
                {
                    GetBangCount(z, x - 1, y, drawing, index);
                }
                if (y < inputY && drawing[z, x, y + 1] == 0)
                {
                    GetBangCount(z, x, y + 1, drawing, index);
                }
                if (y > 1 && drawing[z, x, y - 1] == 0)
                {
                    GetBangCount(z, x, y - 1, drawing, index);
                }
                return true;
            }
            else
            {
                return false;
            }
            
        }
    }
}
